package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"time"
)

type MainController struct {
	beego.Controller
}

type Count struct {
	Sum int
}

type Week struct {
	Days string
	Sum int
}

func (this *MainController) Get() {
	o := orm.NewOrm()
	//统计用户总数
	var userNum []Count
	sql := "select count(*) as sum from `yk_user`"
	_, err := o.Raw(sql).QueryRows(&userNum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}
	//获取时间
	year := time.Now().Year()
	months := time.Now().Month()
	day := time.Now().Day()
	t := time.Date(year, months, day, 0, 0, 0,0, time.Local)
	nowTime := t.Format("2006-01-02")
	//统计今日注册数
	var daySum []Count
	sql = "select count(*) as sum from `yk_user` where created_at>='"+nowTime+"'"
	_, err = o.Raw(sql).QueryRows(&daySum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//油卡订单总数
	var oilSum []Count
	sql = "select count(*) as sum from `yk_recharge_order`"
	_, err = o.Raw(sql).QueryRows(&oilSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//油卡今日充值订单
	var dayOilSum []Count
	sql = "select count(*) as sum from `yk_recharge_order` where created_at>='"+nowTime+"'"
	_, err = o.Raw(sql).QueryRows(&dayOilSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}


	//话费订单总数
	var mobileSum []Count
	sql = "select count(*) as sum from `yk_mobile_recharge_order`"
	_, err = o.Raw(sql).QueryRows(&mobileSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//话费今日充值订单
	var dayMobileSum []Count
	sql = "select count(*) as sum from `yk_mobile_recharge_order` where created_at>='"+nowTime+"'"
	_, err = o.Raw(sql).QueryRows(&dayMobileSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//查询安卓手机注册用户
	var androidSum []Count
	sql = "select count(*) as sum from `yk_user` where app_platform='android'"
	_, err = o.Raw(sql).QueryRows(&androidSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//查询苹果手机注册用户
	var iosSum []Count
	sql = "select count(*) as sum from `yk_user` where app_platform='ios'"
	_, err = o.Raw(sql).QueryRows(&iosSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//其他注册用户
	var otherSum []Count
	sql = "select count(*) as sum from `yk_user` where app_platform!='android' and app_platform!='ios'"
	_, err = o.Raw(sql).QueryRows(&otherSum)
	if err != nil {
		beego.Info("错误信息:", err)
		return
	}

	//查询最近七天的用户注册数据
	var weekDay []Week
	sql = "SELECT a.days,IFNULL(b.count,0) AS sum FROM ( SELECT CURDATE() AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS days ) a LEFT JOIN ( SELECT DATE(created_at) AS DATETIME, COUNT(*) AS COUNT FROM `yk_user` GROUP BY DATE(created_at) ) b ON a.days = b.datetime order by a.days asc"
	_,err = o.Raw(sql).QueryRows(&weekDay)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//查询最近七天的油卡订单数据
	var oilWeekDay []Week
	sql = "SELECT a.days,IFNULL(b.count,0) AS sum FROM ( SELECT CURDATE() AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS days ) a LEFT JOIN ( SELECT DATE(created_at) AS DATETIME, COUNT(*) AS COUNT FROM `yk_recharge_order` GROUP BY DATE(created_at) ) b ON a.days = b.datetime order by a.days asc"
	_, err = o.Raw(sql).QueryRows(&oilWeekDay)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//查询最近七天的话费订单数据
	var mobileWeekDay []Week
	//sql = "SELECT DATE_FORMAT(created_at,'%m-%d') AS days,COUNT(*) AS sum FROM `yk_mobile_recharge_order` WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY)<=DATE(created_at) GROUP BY days"
	sql = "SELECT a.days,IFNULL(b.count,0) AS sum FROM ( SELECT CURDATE() AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS days UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS days ) a LEFT JOIN ( SELECT DATE(created_at) AS DATETIME, COUNT(*) AS COUNT FROM `yk_mobile_recharge_order` GROUP BY DATE(created_at) ) b ON a.days = b.datetime order by a.days asc"
	_, err = o.Raw(sql).QueryRows(&mobileWeekDay)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//查询各套餐订单数

	this.Data["userCount"] = userNum[0].Sum
	this.Data["todayCount"] = daySum[0].Sum
	this.Data["oilNum"] = oilSum[0].Sum
	this.Data["todayOilOrderNum"] = dayOilSum[0].Sum
	this.Data["mobileNum"] = mobileSum[0].Sum
	this.Data["dayMobileOrderSum"] = dayMobileSum[0].Sum
	this.Data["androidSum"] = androidSum[0].Sum
	this.Data["iosSum"] = iosSum[0].Sum
	this.Data["otherSum"] = otherSum[0].Sum

	this.Data["weekUser"] = weekDay
	this.Data["weekOilOrder"] = oilWeekDay
	this.Data["weekmobileOrder"] = mobileWeekDay
	this.TplName = "index.html"
}

func (this *MainController) Quota(){
	o := orm.NewOrm()
	//获取时间
	//year := time.Now().Year()
	//months := time.Now().Month()
	//day := time.Now().Day()
	//t := time.Date(year, months, day, 0, 0, 0,0, time.Local)
	//nowTime := t.Format("2006-01-02")
	//油卡订单总数
	var oilSum []Count
	sql := "select count(*) as sum from `yk_recharge_order`"
	_, err := o.Raw(sql).QueryRows(&oilSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//本月油卡订单
	var monthSum []Count
	sql = "SELECT COUNT(*) AS sum FROM `yk_recharge_order` WHERE DATE_FORMAT( created_at, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )"
	_, err = o.Raw(sql).QueryRows(&monthSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//本月支付订单总数
	var monthPaySum []Count
	sql = "SELECT COUNT(*) AS sum FROM `yk_recharge_order` WHERE DATE_FORMAT( created_at, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) AND status=1"
	_, err = o.Raw(sql).QueryRows(&monthPaySum)
	if err != nil {
		beego.Info("错误信息:", err)
		return
	}

	//本月未支付订单
	var monthNotPaySum []Count
	sql = "SELECT COUNT(*) AS sum FROM `yk_recharge_order` WHERE DATE_FORMAT( created_at, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) AND status=0"
	_, err = o.Raw(sql).QueryRows(&monthNotPaySum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//本月待充值订单
	var monthWaitRechargeSum []Count
	sql = "SELECT COUNT(*) AS sum FROM `yk_recharge_order` WHERE DATE_FORMAT( created_at, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) AND status=2"
	_, err = o.Raw(sql).QueryRows(&monthWaitRechargeSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	//本月已完成订单
	var monthFinishRechargeSum []Count
	sql = "SELECT COUNT(*) AS sum FROM `yk_recharge_order` WHERE DATE_FORMAT( created_at, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' ) AND status=4"
	_, err = o.Raw(sql).QueryRows(&monthFinishRechargeSum)
	if err != nil {
		beego.Info("错误信息:",err)
		return
	}

	this.Data["orderSum"] = oilSum[0].Sum
	this.Data["monthSum"] = monthSum[0].Sum
	this.Data["monthPaySum"] = monthPaySum[0].Sum
	this.Data["monthNotPaySum"] = monthNotPaySum[0].Sum
	this.Data["waitRechargeSum"] = monthWaitRechargeSum[0].Sum
	this.Data["finishRechargeSum"] = monthFinishRechargeSum[0].Sum
	this.TplName = "quota.html"
}

func (this *MainController) Trend(){
	this.TplName = "trend.html"
}

func (this*MainController) Chronic(){
	this.TplName = "chronic.html";
}